<template>
  <div class="com-container">
    <div class="com-chart" ref="chart_ref"></div>
    <span class="iconfont arrow-left" @click="toLeft" :style="comStyle">&#xe6ef;</span>
    <span class="iconfont arrow-right" @click="toRight" :style="comStyle">&#xe6ed;</span>
    <span class="cat-name" :style="comStyle">{{catName}}</span>
  </div>
</template>

<script>
import { mapState } from 'vuex'
import { getThemeValue } from '../utils/theme_utils'
export default {
  data() {
    return {
      chartInstance: null, // echart实例
      allData: null, // 服务器返回的数据
      currentIndex: 0, // 当前所展示出的一级分类数据
      titleFontSize: 0
    }
  },
  computed: {
    catName() {
      if (!this.allData) {
        return ''
      } else {
        return this.allData[this.currentIndex].name
      }
    },
    comStyle() {
      return {
        fontSize: this.titleFontSize + 'px',
        color: getThemeValue(this.theme).titleColor
      }
    },
    ...mapState(['theme'])
  },
  watch: {
    theme() {
      // console.log('主题切换了')
      this.chartInstance.dispose() // 销毁当前图表
      this.initChart() // 从新以最新主题初始化图表对象
      this.screenAdapter() // 完成屏幕适配
      this.updateChart() // 更新图表的展示
    }
  },
  // 在mounted后元素才被挂载上去才能通过ref拿到
  mounted() {
    this.initChart()
    this.getData()
    window.addEventListener('resize', this.screenAdapter)
    // 在页面加载完成的时候，主动进行屏幕的适配
    this.screenAdapter()
  },
  destroyed() {
    // 在组件销毁的时候需要把监听器取消掉，防止内存的泄露
    window.removeEventListener('resize', this.screenAdapter)
  },
  methods: {
    // 初始化echartInstance对象
    initChart() {
      this.chartInstance = this.$echarts.init(this.$refs.chart_ref, this.theme)
      // 对图表初始化配置的控制
      const initOption = {
        title: {
          text: '┃ 热销商品占比',
          left: 20,
          top: 20
        },
        legend: {
          top: '15%',
          icon: 'circle'
        },
        tooltip: {
          show: true,
          formatter: (arg) => {
            const thirdCategory = arg.data.children
            // 计算出所有三级分类的数值总和
            let total = 0
            thirdCategory.forEach(item => {
              total += item.value
            })
            let retStr = ''
            thirdCategory.forEach(item => {
              retStr += `${item.name}:${parseInt(item.value / total * 100) + '%'}<br/>`
            })
            return retStr
          }
        },
        series: [
          {
            type: 'pie',
            label: {
              show: false
            },
            emphasis: { // 饼图高亮状态下的配置
              label: {
                show: true
              },
              labelLine: {
                show: false // 标签的线
              }
            }
          }
        ]
      }
      this.chartInstance.setOption(initOption)
    },
    // 获取服务器数据
    getData() {
      const data = [
        {
          name: '女装',
          children: [
            {
              name: '裙装',
              value: 12312,
              children: [
                { name: '短裙', value: 151 },
                { name: '百褶裙', value: 165 }
              ]
            },
            {
              name: '女士上衣',
              value: 12123,
              children: [
                { name: '保暖上衣', value: 1251 },
                { name: '时尚秋衣', value: 1265 },
                { name: '女士T恤', value: 562 }
              ]
            },
            {
              name: '外套',
              value: 14523,
              children: [
                { name: '摇粒绒', value: 2251 },
                { name: '鹅绒羽绒服', value: 1265 },
                { name: '冲锋衣', value: 1562 }
              ]
            },
            {
              name: '吊带',
              value: 13123,
              children: [
                { name: '睡衣', value: 3251 },
                { name: '情趣吊带', value: 4265 },
                { name: '透明吊带', value: 2562 },
                { name: '黑丝吊带', value: 2512 }
              ]
            },
            {
              name: '内衣',
              value: 23123,
              children: [
                {
                  name: '蕾丝',
                  value: 1323
                },
                {
                  name: '纯棉',
                  value: 631
                }
              ]
            },
            {
              name: '短裤',
              value: 52123,
              children: [
                {
                  name: '破洞短裤',
                  value: 3147
                },
                {
                  name: '透气冰丝',
                  value: 3544
                }
              ]
            }
          ]
        },
        {
          name: '手机数码',
          children: [
            {
              name: '手机',
              value: 5312,
              children: [
                {
                  name: '小米',
                  value: 1032
                },
                {
                  name: 'oppo',
                  value: 2232
                },
                {
                  name: '华为',
                  value: 3301
                },
                {
                  name: '魅族',
                  value: 1515
                }
              ]
            },
            {
              name: '手机配件',
              value: 4123,
              children: [
                {
                  name: '屏幕',
                  value: 330
                },
                {
                  name: '电池',
                  value: 151
                }
              ]
            },
            {
              name: '摄影摄像',
              value: 3523,
              children: [
                {
                  name: '索尼',
                  value: 3301
                },
                {
                  name: '莱卡',
                  value: 1515
                }
              ]
            },
            {
              name: '影音娱乐',
              value: 3123,
              children: [
                {
                  name: '暴风影音',
                  value: 3301
                },
                {
                  name: '小米音响',
                  value: 1515
                },
                {
                  name: '快播',
                  value: 3301
                },
                {
                  name: 'uc',
                  value: 1515
                }
              ]
            },
            {
              name: '数码配件',
              value: 2123,
              children: [
                {
                  name: '摄像头',
                  value: 3301
                },
                {
                  name: '防尘罩',
                  value: 1515
                }
              ]
            }, {
              name: '智能设备',
              value: 2956,
              children: [
                {
                  name: '小爱同学',
                  value: 3301
                },
                {
                  name: 'switch',
                  value: 1515
                }
              ]
            }, {
              name: '其它',
              value: 1234,
              children: [
                {
                  name: '皮肤',
                  value: 3301
                },
                {
                  name: '会员',
                  value: 1515
                }
              ]
            }
          ]
        },
        {
          name: '美妆护肤',
          children: [{
            name: '面部护肤',
            value: 22312,
            children: [
              {
                name: '护肤宝',
                value: 3301
              },
              {
                name: '精华面膜',
                value: 1515
              }
            ]
          },
          {
            name: '彩妆',
            value: 8123,
            children: [
              {
                name: '眼影',
                value: 3301
              },
              {
                name: '粉底液',
                value: 1515
              }
            ]
          },
          {
            name: '男士护肤',
            value: 4523,
            children: [
              {
                name: '男士洗面奶',
                value: 3301
              },
              {
                name: '防嗮喷雾',
                value: 1515
              }
            ]
          },
          {
            name: '美妆工具',
            value: 5123,
            children: [
              {
                name: '化妆镜',
                value: 3301
              },
              {
                name: '睫毛夹',
                value: 1515
              }
            ]
          },
          {
            name: '其它',
            value: 2123,
            children: [
              {
                name: '芦荟胶',
                value: 3301
              },
              {
                name: '面膜',
                value: 1515
              }
            ]
          }]
        }
      ]
      this.allData = data
      this.updateChart()
    },
    // 更新图表
    updateChart() {
      const legendData = this.allData[this.currentIndex].children.map(item => {
        return item.name
      })
      const seriesData = this.allData[this.currentIndex].children.map(item => {
        return {
          name: item.name,
          value: item.value,
          children: item.children
        }
      })
      const dataoption = {
        legend: {
          data: legendData
        },
        series: [{
          data: seriesData
        }]
      }
      this.chartInstance.setOption(dataoption)
    },
    // 当浏览器大小发生变化的时候，会调用该方法来适配屏幕
    screenAdapter() {
      this.titleFontSize = this.$refs.chart_ref.offsetWidth / 100 * 3.6 // 这个比例比较合适
      // 和分辨率大小相关的配置项
      const adapterOption = {
        title: {
          textStyle: {
            fontSize: this.titleFontSize
          }
        },
        legend: {
          itemWidth: this.titleFontSize,
          itemHeight: this.titleFontSize,
          itemGap: this.titleFontSize / 2, // 图例间隔
          textStyle: {
            fontSize: this.titleFontSize / 2
          }
        },
        series: [
          {
            radius: this.titleFontSize * 4.5, // 设置饼图半径
            center: ['50%', '60%']
          }
        ]
      }
      this.chartInstance.setOption(adapterOption)
      // 需要手动调用图表对象的resize方法才能产生效果
      this.chartInstance.resize()
    },
    // 点击左箭头
    toLeft() {
      this.currentIndex--
      if (this.currentIndex < 0) {
        this.currentIndex = this.allData.length - 1
      }
      this.updateChart()
    },
    // 点击右箭头
    toRight() {
      this.currentIndex++
      if (this.currentIndex > this.allData.length - 1) {
        this.currentIndex = 0
      }
      this.updateChart()
    }
  }
}
</script>

<style lang="less" scoped>
.arrow-left {
  position: absolute;
  left: 10%;
  top: 50%;
  transform: translateY(-50%);
  cursor: pointer;
  color: white;
}

.arrow-right {
  position: absolute;
  right: 10%;
  top: 50%;
  transform: translateY(-50%);
  cursor: pointer;
  color: white;
}

.cat-name {
  position: absolute;
  left: 80%;
  bottom: 20px;
  color: white;
}
</style>
